package com.darly.book.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.darly.book.BookController;
import com.darly.book.R;
import com.darly.book.model.bean.BookListDetailBean;
import com.darly.book.presenter.BookListDetailPresenter;
import com.darly.book.presenter.contract.BookListDetailContract;
import com.darly.book.ui.adapter.BookListDetailAdapter;
import com.darly.book.ui.base.BaseMVPActivity;
import com.darly.book.utils.Constant;
import com.darly.book.widget.RefreshLayout;
import com.darly.book.widget.adapter.WholeAdapter;
import com.darly.book.widget.itemdecoration.DividerItemDecoration;
import com.darly.book.widget.transform.CircleTransform;
import com.darly.widget.titlebar.TitleBar;

import java.util.ArrayList;
import java.util.List;

public class BookListDetailActivity extends BaseMVPActivity<BookListDetailContract.Presenter> implements BookListDetailContract.View {

    private static final String TAG = "BookListDetailActivity";
    private static final String EXTRA_DETAIL_ID = "extra_detail_id";
    RefreshLayout mRefreshLayout;
    RecyclerView mRvContent;
    /****************************/
    private BookListDetailAdapter mDetailAdapter;
    private DetailHeader mDetailHeader;
    private List<BookListDetailBean.BooksBean> mBooksList;
    /***********params****************/
    private String mDetailId;
    private int start = 0;
    private int limit = 20;

    public static void startActivity(Context context, String detailId) {
        Intent intent = new Intent(context, BookListDetailActivity.class);
        intent.putExtra(EXTRA_DETAIL_ID, detailId);
        context.startActivity(intent);
    }

    @Override
    protected int getContentId() {
        return R.layout.activity_refresh_list;
    }


    @Override
    protected BookListDetailContract.Presenter bindPresenter() {
        return new BookListDetailPresenter();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        if (savedInstanceState != null) {
            mDetailId = savedInstanceState.getString(EXTRA_DETAIL_ID);
        } else {
            mDetailId = getIntent().getStringExtra(EXTRA_DETAIL_ID);
        }
        mRefreshLayout = findViewById(R.id.refresh_layout);
        mRvContent = findViewById(R.id.refresh_rv_content);
    }

    @Override
    protected void setUpToolbar(TitleBar toolbar) {
        super.setUpToolbar(toolbar);
        getSupportActionBar().setTitle("书单详情");
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        setUpAdapter();
    }

    private void setUpAdapter() {
        mDetailAdapter = new BookListDetailAdapter(this, new WholeAdapter.Options());
        mDetailHeader = new DetailHeader();
        mDetailAdapter.addHeaderView(mDetailHeader);

        mRvContent.setLayoutManager(new LinearLayoutManager(this));
        mRvContent.addItemDecoration(new DividerItemDecoration(this));
        mRvContent.setAdapter(mDetailAdapter);
    }

    @Override
    protected void initClick() {
        super.initClick();
        mDetailAdapter.setOnLoadMoreListener(
                () -> loadBook()
        );
        mDetailAdapter.setOnItemClickListener((v, p) -> {
            onItemClick(v, p);
        });
    }

    protected void onItemClick(View v, int p) {
        BookListDetailBean.BooksBean.BookBean book = mBooksList.get(p).getBook();
        BookDetailActivity.startActivity(this, book.get_id());
    }

    @Override
    protected void processLogic() {
        super.processLogic();
        mRefreshLayout.showLoading();
        mPresenter.refreshBookListDetail(mDetailId);
    }

    @Override
    public void finishRefresh(BookListDetailBean bean) {
        mDetailHeader.setBookListDetail(bean);
        mBooksList = bean.getBooks();
        refreshBook();
    }

    private void refreshBook() {
        start = 0;
        List<BookListDetailBean.BooksBean.BookBean> books = getBookList();
        mDetailAdapter.refreshItems(books);
        start = books.size();
    }

    private void loadBook() {
        List<BookListDetailBean.BooksBean.BookBean> books = getBookList();
        mDetailAdapter.addItems(books);
        start += books.size();
    }

    private List<BookListDetailBean.BooksBean.BookBean> getBookList() {
        int end = start + limit;
        if (end > mBooksList.size()) {
            end = mBooksList.size();
        }
        List<BookListDetailBean.BooksBean.BookBean> books = new ArrayList<>(limit);
        for (int i = start; i < end; ++i) {
            books.add(mBooksList.get(i).getBook());
        }
        return books;
    }

    @Override
    public void showError() {
        mRefreshLayout.showError();
    }

    @Override
    public void complete() {
        mRefreshLayout.showFinish();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(EXTRA_DETAIL_ID, mDetailId);
    }


    class DetailHeader implements WholeAdapter.ItemView {
        TextView tvTitle;
        TextView tvDesc;
        ImageView ivPortrait;
        TextView tvCreate;
        TextView tvAuthor;
        TextView tvShare;
        BookListDetailBean detailBean;

        @Override
        public View onCreateView(ViewGroup parent) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.header_book_list_detail, parent, false);
            return view;
        }

        @Override
        public void onBindView(View view) {

            tvTitle = view.findViewById(R.id.book_list_info_tv_title);
            tvDesc = view.findViewById(R.id.book_list_detail_tv_desc);
            ivPortrait = view.findViewById(R.id.book_list_info_iv_cover);
            tvCreate = view.findViewById(R.id.book_list_detail_tv_create);
            tvAuthor = view.findViewById(R.id.book_list_info_tv_author);
            tvShare = view.findViewById(R.id.book_list_detail_tv_share);

            //如果没有值就直接返回
            if (detailBean == null) {
                return;
            }
            //标题
            tvTitle.setText(detailBean.getTitle());
            //描述
            tvDesc.setText(detailBean.getDesc());
            //头像
            Glide.with(BookController.getInstance().getContext())
                    .load(Constant.IMG_BASE_URL + detailBean.getAuthor().getAvatar())
                    .placeholder(R.drawable.ic_loadding)
                    .error(R.drawable.ic_load_error)
                    .transform(new CircleTransform(BookController.getInstance().getContext()))
                    .into(ivPortrait);
            //作者
            tvAuthor.setText(detailBean.getAuthor().getNickname());
        }

        public void setBookListDetail(BookListDetailBean bean) {
            detailBean = bean;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
